import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Card {
    private String suit;//花色
    private int rank;//数字

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "["+ "花色=" + suit + "数字=" + rank +"]";
    }
}
public class CardDemo {
    private static final String[] SUITS = {"♥","♠","♦","♣"};
    public static List<Card> buyCard() {
        List<Card> cards = new ArrayList<>();
        for(int i = 0;i < 4;i++) {
            for (int j = 1; j <= 13; j++) {
                String suit = SUITS[i];
                int rank = j;
                Card card = new Card(suit,rank);
                cards.add(card);
            }
        }
        return cards;
    }
    //洗牌
    private static void shuffle(List<Card> cards) {
        //从后往前生成随机下标和当前下标换位
        Random random = new Random();
        for (int i = cards.size()-1; i > 0 ; i--) {
            int index = random.nextInt(i);
            Card temp = cards.get(i);
            cards.set(i,cards.get(index));
            cards.set(index,temp);
        }
    }

    public static void main(String[] args) {
        //买牌
        List<Card> cardList = buyCard();
        System.out.println("买牌:" + cardList);
        //洗牌
        shuffle(cardList);
        System.out.println("洗牌后:" + cardList);
        //揭牌
        List<List<Card>> hands = new ArrayList<>();

        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);
        //三个人轮流抓5张牌
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardList.remove(0);//揭牌相当于把牌从牌库删除，每次删除0下标的牌
                hands.get(j).add(card);//将牌给每个人
            }

        }
        System.out.println("第一个人的牌:" + hand1);
        System.out.println("第二个人的牌:" + hand2);
        System.out.println("第三个人的牌:" + hand3);
        System.out.println("剩下的牌:" + cardList);
    }
}
